from django.contrib import auth
from django.contrib.auth.base_user import AbstractBaseUser
from django.db import models
from django.contrib.auth.models import AbstractUser, Group, Permission, _user_get_all_permissions, _user_has_perm, \
    _user_has_module_perms, PermissionsMixin
from meiduo_mall.utils.models import BaseModel
from areas.models import Area


# 创建user用户的模型类，用于生成Mysql表
class User(AbstractUser):
    # 由于父类中没有mobile， 所以需要添加mobile手机号
    mobile = models.CharField(max_length=11, verbose_name='手机号')
    email_active = models.BooleanField(default=False, verbose_name="邮件激活")
    # 关联一个默认收货地址
    default_address = models.ForeignKey('users.Address', related_name='users', verbose_name='默认收货地址', null=True, on_delete=models.CASCADE)
    # 是否是超级管理员权限
    is_superuser = models.IntegerField(default=1, verbose_name='超级管理员', )
    # 是否是商家店员
    is_staff = models.IntegerField(default=1, verbose_name='商家店员', )

    # 生成的表
    class Meta:
        db_table = 'users_tb'
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


# 创建用户的收货地址
class Address(BaseModel):
    # 创建字段
    """用户地址"""
    # 用户关联外键用户表
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='addresses', verbose_name='用户')
    title = models.CharField(max_length=32, verbose_name='地址名称')
    receiver = models.CharField(max_length=32, verbose_name='收货人')
    # 省关联地址表中的省，市，区
    province = models.ForeignKey(Area, on_delete=models.PROTECT, related_name='province_addresses',
                                 verbose_name='省')
    city = models.ForeignKey(Area, on_delete=models.PROTECT, related_name='city_addresses', verbose_name='市')
    district = models.ForeignKey(Area, on_delete=models.PROTECT, related_name='district_addresses',
                                 verbose_name='区')
    # 详细地址
    place = models.CharField(max_length=64, verbose_name='地址')
    # 手机号
    mobile = models.CharField(max_length=11, verbose_name='手机')
    # 固定电话
    tel = models.CharField(max_length=20, null=True, blank=True, default='', verbose_name='固定电话')
    # 电子邮件
    email = models.CharField(max_length=30, null=True, blank=True, default='', verbose_name='电子邮箱')
    # 是否逻辑删除
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_address'
        verbose_name = '用户地址'
        verbose_name_plural = verbose_name
        # 排序
        ordering = ['-update_time']

# test
# class PermissionsMixin_context(models.Model):
#     """
#     A mixin class that adds the fields and methods necessary to support
#     Django's Group and Permission model using the ModelBackend.
#     """
#     is_superuser = models.BooleanField(('superuser status'), default=False,
#                                        help_text=('Designates that this user has all permissions without '
#                                                   'explicitly assigning them.'))
#     groups = models.ManyToManyField(Group, verbose_name=('groups'),
#                                     blank=True, help_text=('The groups this user belongs to. A user will '
#                                                            'get all permissions granted to each of '
#                                                            'their groups.'),
#                                     related_name="user_set1", related_query_name="user")
#     user_permissions = models.ManyToManyField(Permission,
#                                               verbose_name=('user permissions'), blank=True,
#                                               help_text=('Specific permissions for this user.'),
#                                               related_name="user_set1", related_query_name="user")
#
#     user_nums = models.CharField(max_length=32)
#
#     class Meta:
#         db_table = 'tb_user_manager'
#         verbose_name = '用户管理中心'
